package com.iw.adoms.service;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import com.iw.adoms.dao.GoodsDao;
import com.iw.adoms.dao.QrGoodsCodeDao;
import com.iw.adoms.entity.Goods;
import com.iw.adoms.entity.QrGoodsCode;
import com.iw.adoms.entity.ShopSalesRecord;
import com.iw.adoms.entity.neum.Status;
import com.iw.core.orm.hibernate.BaseDao;
import com.iw.core.orm.hibernate.BaseService;

@Service
@Transactional
public class ShopSalesRecordService extends BaseService<ShopSalesRecord> {

	@Resource
	private GoodsDao goodsDao;

	@Resource
	private QrGoodsCodeDao qrGoodsCodeDao;

	@Override
	@Resource(name = "shopSalesRecordDao")
	public void setBaseDao(BaseDao<ShopSalesRecord> baseDao) {
		this.baseDao = baseDao;
	}

	public void sale(Model model, ShopSalesRecord shopSalesRecord) {
		// 保存销售记录
		shopSalesRecord.setCode("S" + new Date().getTime());
		shopSalesRecord.setSaleDate(new Date());
		this.save(shopSalesRecord);
		// 更新goods表中的状态为：已售
		Goods goods = goodsDao.find(shopSalesRecord.getGoods().getId());
		goods.setState(Status.HAS_SALED.getValue());
		goodsDao.update(goods);
		// 更新二维码表的状态:可积分
		QrGoodsCode qrCode = goods.getQrCode();
		qrCode.setState(Status.QR_ABLE_INTEGRAL.getValue());
		qrGoodsCodeDao.update(qrCode);
		model.addAttribute("msg","操作成功！");
	}
}
